home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C26 / ExtractInfo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  5.5 KB  |  174 lines

  1. //: C26:ExtractInfo.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Extracts all the information from a CGI POST
  7. // submission, generates a file and stores the
  8. // information on the server. By generating a 
  9. // unique file name, there are no clashes like
  10. // you get when storing to a single file.
  11. #include "CGImap.h"
  12. #include <iostream>
  13. #include <fstream>
  14. #include <cstdio>
  15. #include <ctime>
  16. using namespace std;
  17.  
  18. const string contact("Bruce@EckelObjects.com");
  19. // Paths in this program are for Linux/Unix. You
  20. // must use backslashes (two for each single 
  21. // slash) on Win32 servers:
  22. const string rootpath("/home/eckel/");
  23.  
  24. void show(CGImap& m, ostream& o);
  25. // The definition for the following is the only
  26. // thing you must change to customize the program
  27. void 
  28. store(CGImap& m, ostream& o, string nl = "\n");
  29.  
  30. int main() {
  31.   cout << "Content-type: text/html\n"<< endl;
  32.   Post p; // Collect the POST data
  33.   CGImap query(p);
  34.   // "test-field" set to "on" will dump contents
  35.   if(query["test-field"] == "on") {
  36.     cout << "map size: " << query.size() << "<br>";
  37.     query.dump(cout);
  38.   }
  39.   if(query["subject-field"].size() == 0) {
  40.     cout << "<h2>Incorrect form. Contact " <<
  41.     contact << endl;
  42.     return 0;
  43.   }
  44.   string email = query["email-address"];
  45.   if(email.size() == 0) {
  46.     cout << "<h2>Please enter your email address"
  47.       << endl;
  48.     return 0;
  49.   }
  50.   if(email.find_first_of(" \t") != string::npos){
  51.     cout << "<h2>You cannot include white space "
  52.       "in your email address" << endl;
  53.     return 0;
  54.   }
  55.   if(email.find('@') == string::npos) {
  56.     cout << "<h2>You must include a proper email"
  57.       " address including an '@' sign" << endl;
  58.     return 0;
  59.   }
  60.   if(email.find('.') == string::npos) {
  61.     cout << "<h2>You must include a proper email"
  62.       " address including a '.'" << endl;
  63.     return 0;
  64.   }
  65.   // Create a unique file name with the user's
  66.   // email address and the current time in hex
  67.   const int bsz = 1024;
  68.   char fname[bsz];
  69.   time_t now;
  70.   time(&now); // Encoded date & time
  71.   sprintf(fname, "%s%X.txt", email.c_str(), now);
  72.   string path(rootpath + query["subject-field"] +
  73.      "/" + fname);
  74.   ofstream out(path.c_str());
  75.   if(!out) {
  76.     cout << "cannot open " << path << "; Contact"
  77.       << contact << endl;
  78.     return 0;
  79.   }
  80.   // Store the file and path information:
  81.   out << "///{" << path << endl;
  82.   // Display optional reminder:
  83.   if(query["reminder"].size() != 0)
  84.     cout <<"<H1>" << query["reminder"] <<"</H1>";
  85.   show(query, cout); // For results page
  86.   store(query, out); // Stash data in file
  87.   cout << "<br><H2>Your submission has been "
  88.     "posted as<br>" << fname << endl 
  89.     << "<br>Thank you</H2>" << endl;
  90.   out.close();
  91.   // Optionally send generated file as email
  92.   // to recipients specified in the field:
  93.   if(query["mail-copy"].length() != 0 &&
  94.      query["mail-copy"] != "no") {
  95.     string to = query["mail-copy"];
  96.     // Parse out the recipient names, separated 
  97.     // by ';', into a vector.
  98.     vector<string> recipients;
  99.     int ii = to.find(';');
  100.     while(ii != string::npos) {
  101.       recipients.push_back(to.substr(0, ii));
  102.       to = to.substr(ii + 1);
  103.       ii = to.find(';');
  104.     }
  105.     recipients.push_back(to); // Last one
  106.     // "fastmail" only available on Linux/Unix:
  107.     for(int i = 0; i < recipients.size(); i++) {
  108.       string cmd("fastmail -s"" \"" +
  109.         query["subject-field"] + "\" " +
  110.         path + " " + recipients[i]);
  111.       system(cmd.c_str());
  112.     }
  113.   }
  114.   // Execute a confirmation program on the file.
  115.   // Typically, this is so you can email a
  116.   // processed data file to the client along with
  117.   // a confirmation message:
  118.   if(query["confirmation"].length() != 0) {
  119.     string conftype = query["confirmation"];
  120.     if(conftype == "confirmation1") {
  121.       string command("./ProcessApplication.exe "+
  122.         path + " &");
  123.       // The data file is the argument, and the
  124.       // ampersand runs it as a separate process:
  125.       system(command.c_str());
  126.       string logfile("Extract.log");
  127.       ofstream log(logfile.c_str());
  128.     }
  129.   }
  130. }
  131.  
  132. // For displaying the information on the html 
  133. // results page:
  134. void show(CGImap& m, ostream& o) {
  135.   string nl("<br>");
  136.   o << "<h2>The data you entered was:"
  137.     << "</h2><br>"
  138.     << "From[" << m["email-address"] << ']' <<nl;
  139.   for(CGImap::iterator it = m.begin();
  140.     it != m.end(); it++) {
  141.     string name = (*it).first, 
  142.       value = (*it).second;
  143.     if(name != "email-address" && 
  144.        name != "confirmation" &&
  145.        name != "submit" &&
  146.        name != "mail-copy" &&
  147.        name != "test-field" &&
  148.        name != "reminder")
  149.       o << "<h3>" << name << ": </h3>" 
  150.         << "<pre>" << value << "</pre>";
  151.   }
  152. }
  153.  
  154. // Change this to customize the program:
  155. void store(CGImap& m, ostream& o, string nl) {
  156.   o << "From[" << m["email-address"] << ']' <<nl;
  157.   for(CGImap::iterator it = m.begin();
  158.     it != m.end(); it++) {
  159.     string name = (*it).first, 
  160.       value = (*it).second;
  161.     if(name != "email-address" && 
  162.        name != "confirmation" &&
  163.        name != "submit" &&
  164.        name != "mail-copy" &&
  165.        name != "test-field" &&
  166.        name != "reminder")
  167.       o << nl << "[{[" << name << "]}]" << nl
  168.         << "[([" << nl << value << nl << "])]"
  169.         << nl;
  170.     // Delimiters were added to aid parsing of
  171.     // the resulting text file.
  172.   }
  173. } ///:~
  174.